home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / winmess / winmess.pas next >
Pascal/Delphi Source File  |  1996-04-08  |  2KB  |  78 lines

  1. {
  2.  Designer:  Craig Ward (100554,2072)
  3.  Date:      20/7/95
  4.  
  5.  Function:  Example of dealing with Windows Messages
  6.  
  7. ***************************************************}
  8. unit Winmess;
  9.  
  10. interface
  11.  
  12. uses
  13.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  14.   Forms, Dialogs, StdCtrls;
  15.  
  16. type
  17.   TForm1 = class(TForm)
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.     { Private declarations }
  21.     {This is the procedure declaration for dealing with the Window's message to
  22.      close the app's window - note that we would expect message handler's to be
  23.      declared privately (why would we ever want an external unit to access another unit's
  24.      message handlers!!!!)}
  25.     procedure custWMSYS(var Message: TWMSYSCOMMAND); Message WM_CLOSE;
  26.   public
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. {******************************************************************************}
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. {procedure that deals with the windows WM_CLOSE message -
  39.  
  40.  I'm sure that there are far easier ways of dealing with posting records, but
  41.  this way illustrates some of the concepts behind Windows. Of course, in response
  42.  to "mrYes" we could simply use "close;" but I've included a PostMessage method
  43.  that provides an example of how to send messages to Windows.
  44.  
  45.  Note that this subroutine could be useful for database applications, where the
  46.  a record has not yet been posted and the user tries to close down the form}
  47. procedure TForm1.custWMSYS(var Message: TWMSYSCOMMAND);
  48. var
  49.  sTitle: string;
  50.  pTitle: PChar;
  51.  iTitle: integer;
  52. begin
  53.  {find title of the Form}
  54.  sTitle := Form1.Caption;
  55.   {now set case statement for user's response to dialog box}
  56.   case messageDlg('Save changes?', mtWarning, [mbYes, mbNo, mbCancel], 0) of
  57.     mrYes:
  58.      begin
  59.         {*********************************}
  60.           {allocate room on buffer for pchar}
  61.           pTitle := StrAlloc(256);
  62.           {convert string to pchar}
  63.           StrPCopy(pTitle, sTitle);
  64.           {find window's handle}
  65.           iTitle := FindWindow(nil, pTitle);
  66.         {*********************************}
  67.        {post message to Windows to close down the window}
  68.        PostMessage(iTitle, WM_QUIT, 0, 0);
  69.      end;
  70.    mrNo:
  71.       close;
  72.    mrCancel:
  73.       {do nothing}
  74.   end;
  75. end;
  76.  
  77. end.
  78.